from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QWidget, QCheckBox, QApplication, QPushButton, QHBoxLayout, QVBoxLayout, QTextEdit, \
    QTableWidgetItem, QLabel, QLineEdit
from PySide2.QtCore import Qt

from robotController import share
from robotController.binding import formula_share
from robotController.binding.home_bind import home_bind
from robotController.util.requestUtil import requestUtil


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.setWindowFlags(Qt.ToolTip)

        organizationList = share.organization.list

        # self.resize(200,300)
        self.setMaximumSize(500,700);
        self.setMinimumSize(500,700);
        self.setWindowTitle('组织选择')
        self.vbox = QVBoxLayout()
        self.qc = []
        # self.vbox.addWidget(self.cb1)
        for item in organizationList:
            qCheckBox = QCheckBox(item["name"],self)
            self.vbox.addWidget(qCheckBox)
            self.qc.append(qCheckBox)

        self.vbox1 = QVBoxLayout()
        bt = QPushButton('确定',self)
        et = QPushButton('取消',self)
        bt.clicked.connect(lambda : self.affirm())
        et.clicked.connect(lambda : self.cancel())
        self.vbox1.addWidget(bt)
        self.vbox1.addWidget(et)

        self.hbox = QHBoxLayout()
        self.hbox.addLayout(self.vbox)
        self.hbox.addLayout(self.vbox1)
        self.setLayout(self.hbox)
        self.show()

    def affirm(self):
        affirmOrganization = ""
        for item in self.qc:
            if item.isChecked():
                affirmOrganization = affirmOrganization + item.text() + "/"

        affirmOrganization = affirmOrganization[:-1]

        # 将字符填入对应单元格
        tableWidget = share.SI.formula.ui.formula_tableWidget
        try:

            # 向单元格插入内容
            tableWidget.setItem(formula_share.cell.cellRow, 1, QTableWidgetItem(affirmOrganization))
        except Exception as e:
            print(e)

        self.close()

    def cancel(self):
        self.close()

# 新建分组窗口
class Create_Group_POP(QWidget):
    def __init__(self,parent=None):
        super(Create_Group_POP, self).__init__(parent)
        # 新建窗口存留
        share.window.Create_Group_POP = self
        # 设置窗口大小
        self.setFixedSize(200, 150);
        # 设置题头
        self.setWindowTitle("新建分组")
        # 隐藏最大化最小化
        self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint | QtCore.Qt.WindowStaysOnTopHint)
        # 添加框组件绑定（任务名，备注） 最大总体布局为纵向布局
        vLayout = QVBoxLayout()
        # 创建填写布局
        # 名称
        vLayout.addWidget(self.group_name_module())
        # 备注
        vLayout.addWidget(self.group_code_module())
        # 创建按钮组（确定，取消）
        vLayout.addWidget(self.buttons())
        self.setLayout(vLayout)

    # 添加分组名组件
    def group_name_module(self):
        widget = QWidget()
        # 任务名
        group_name_label = QLabel()
        group_name_label.setText("分组名称")
        # 创建任务名填写框
        group_name_LineEdit = QLineEdit(self)
        self.group_name_LineEdit = group_name_LineEdit
        group_name_LineEdit.setObjectName("group_name_LineEdit")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(group_name_label)
        hLayout1.addWidget(group_name_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 添加分组编码组件
    def group_code_module(self):
        widget = QWidget()
        # 任务名
        group_code_label = QLabel()
        group_code_label.setText("分组编码")
        # 创建任务名填写框
        group_code_LineEdit = QLineEdit(self)
        self.group_code_LineEdit = group_code_LineEdit
        group_code_LineEdit.setObjectName("group_code_LineEdit")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(group_code_label)
        hLayout1.addWidget(group_code_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 确定取消按钮组
    def buttons(self):
        widget = QWidget()
        # 确定
        confirmBtn = QPushButton('确定',self)
        confirmBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        confirmBtn.clicked.connect(lambda : self.create_group_confirm())

        # 取消
        cancelBtn = QPushButton('取消')
        cancelBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        cancelBtn.clicked.connect(self.close)

        # 确认删除按钮布局
        hLayout = QHBoxLayout()
        hLayout.setObjectName("buttonLayout")
        hLayout.addStretch();
        hLayout.addWidget(confirmBtn)
        hLayout.addWidget(cancelBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 新建分组事件
    def create_group_confirm(self):
        name = self.findChild(QLineEdit,"group_name_LineEdit").text()
        code = self.findChild(QLineEdit,"group_code_LineEdit").text()

        data = {
            "name": name,
            "code": code
        }

        dataMap = requestUtil("/template/group/create","POST",data).dataMap

        if dataMap["flag"]:
            # 关闭本窗口
            self.close()
            # 成功提示
            QtWidgets.QMessageBox.about(self ,"添加成功", dataMap["message"])
            # 重新渲染单位及分组下拉列表
            home_bind().ug_comboBox_value_fun()
            # 新增成功刷新数据
            from robotController.binding.template_group_bind import group_data_bind
            group_data_bind()
        else:
            QtWidgets.QMessageBox.about(self ,"添加失败", dataMap["message"])

# 修改分组窗口
class Update_Group_POP(QWidget):
    def __init__(self,id,parent=None):
        super(Update_Group_POP, self).__init__(parent)
        self.id = id
        # 新建窗口存留
        share.window.Update_Group_POP = self
        # 设置窗口大小
        self.setFixedSize(200, 150);
        # 设置题头
        self.setWindowTitle("修改分组")
        # 隐藏最大化最小化
        self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint | QtCore.Qt.WindowStaysOnTopHint)
        # 添加框组件绑定（任务名，备注） 最大总体布局为纵向布局
        vLayout = QVBoxLayout()
        # 创建填写布局
        # 名称
        vLayout.addWidget(self.group_name_module())
        # 备注
        vLayout.addWidget(self.group_code_module())
        # 创建按钮组（确定，取消）
        vLayout.addWidget(self.buttons())
        self.setLayout(vLayout)

    # 添加分组名组件
    def group_name_module(self):
        widget = QWidget()
        # 任务名
        group_name_label = QLabel()
        group_name_label.setText("分组名称")
        # 创建任务名填写框
        group_name_LineEdit = QLineEdit(self)
        self.group_name_LineEdit = group_name_LineEdit
        group_name_LineEdit.setObjectName("group_name_LineEdit")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(group_name_label)
        hLayout1.addWidget(group_name_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 添加分组编码组件
    def group_code_module(self):
        widget = QWidget()
        # 任务名
        group_code_label = QLabel()
        group_code_label.setText("分组编码")
        # 创建任务名填写框
        group_code_LineEdit = QLineEdit(self)
        self.group_code_LineEdit = group_code_LineEdit
        group_code_LineEdit.setObjectName("group_code_LineEdit")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(group_code_label)
        hLayout1.addWidget(group_code_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 确定取消按钮组
    def buttons(self):
        widget = QWidget()
        # 确定
        confirmBtn = QPushButton('确定',self)
        confirmBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        confirmBtn.clicked.connect(lambda : self.create_group_confirm())

        # 取消
        cancelBtn = QPushButton('取消')
        cancelBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        cancelBtn.clicked.connect(self.close)

        # 确认删除按钮布局
        hLayout = QHBoxLayout()
        hLayout.setObjectName("buttonLayout")
        hLayout.addStretch();
        hLayout.addWidget(confirmBtn)
        hLayout.addWidget(cancelBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 修改分组事件
    def create_group_confirm(self):
        name = self.findChild(QLineEdit,"group_name_LineEdit").text()
        code = self.findChild(QLineEdit,"group_code_LineEdit").text()

        data = {
            "id":self.id,
            "name": name,
            "code": code
        }

        dataMap = requestUtil("/template/group/update","PUT",data).dataMap

        if dataMap["flag"]:
            # 关闭本窗口
            self.close()
            # 成功提示
            QtWidgets.QMessageBox.about(self ,"修改成功", dataMap["message"])
            # 重新渲染单位及分组下拉列表
            home_bind().ug_comboBox_value_fun()
            # 新增成功刷新数据
            from robotController.binding.template_group_bind import group_data_bind
            group_data_bind()
        else:
            QtWidgets.QMessageBox.about(self ,"修改失败", dataMap["message"])

# 新建单位窗口
class Create_Unit_POP(QWidget):
    def __init__(self,parent=None):
        super(Create_Unit_POP, self).__init__(parent)
        # 设置窗口大小
        self.setFixedSize(200, 150);
        # 设置题头
        self.setWindowTitle("新建单位")
        # 隐藏最大化最小化
        self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint | QtCore.Qt.WindowStaysOnTopHint)
        # 添加框组件绑定（任务名，备注） 最大总体布局为纵向布局
        vLayout = QVBoxLayout()
        # 创建填写布局
        # 名称
        vLayout.addWidget(self.unit_name_module())
        # 备注
        vLayout.addWidget(self.unit_code_module())
        # 创建按钮组（确定，取消）
        vLayout.addWidget(self.buttons())
        self.setLayout(vLayout)

    # 添加单位名组件
    def unit_name_module(self):
        widget = QWidget()
        # 单位名
        unit_name_label = QLabel()
        unit_name_label.setText("单位名称")
        # 创建单位名填写框
        unit_name_LineEdit = QLineEdit(self)
        self.unit_name_LineEdit = unit_name_LineEdit
        unit_name_LineEdit.setObjectName("unit_name_LineEdit")
        # 进行单位名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(unit_name_label)
        hLayout1.addWidget(unit_name_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 添加单位编码组件
    def unit_code_module(self):
        widget = QWidget()
        # 单位编码
        unit_code_label = QLabel()
        unit_code_label.setText("单位编码")
        # 创建单位编码填写框
        unit_code_LineEdit = QLineEdit(self)
        self.unit_code_LineEdit = unit_code_LineEdit
        unit_code_LineEdit.setObjectName("unit_code_LineEdit")
        # 进行单位编码及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(unit_code_label)
        hLayout1.addWidget(unit_code_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 确定取消按钮组
    def buttons(self):
        widget = QWidget()
        # 确定
        confirmBtn = QPushButton('确定',self)
        confirmBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        confirmBtn.clicked.connect(lambda : self.create_unit_confirm())

        # 取消
        cancelBtn = QPushButton('取消')
        cancelBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        cancelBtn.clicked.connect(self.close)

        # 确认删除按钮布局
        hLayout = QHBoxLayout()
        hLayout.setObjectName("buttonLayout")
        hLayout.addStretch();
        hLayout.addWidget(confirmBtn)
        hLayout.addWidget(cancelBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 新建单位事件
    def create_unit_confirm(self):
        name = self.findChild(QLineEdit,"unit_name_LineEdit").text()
        code = self.findChild(QLineEdit,"unit_code_LineEdit").text()

        data = {
            "name": name,
            "code": code
        }

        dataMap = requestUtil("/template/unit/create","POST",data).dataMap

        if dataMap["flag"]:
            # 关闭本窗口
            self.close()
            # 成功提示
            QtWidgets.QMessageBox.about(self ,"添加成功", dataMap["message"])
            # 重新渲染单位及分组下拉列表
            home_bind().ug_comboBox_value_fun()
            # 新增成功刷新数据
            from robotController.binding.template_group_bind import unit_data_bind
            unit_data_bind()
        else:
            QtWidgets.QMessageBox.about(self ,"添加失败", dataMap["message"])

# 修改分组窗口
class Update_Unit_POP(QWidget):
    def __init__(self,id,parent=None):
        super(Update_Unit_POP, self).__init__(parent)
        self.id = id
        # 设置窗口大小
        self.setFixedSize(200, 150);
        # 设置题头
        self.setWindowTitle("修改分组")
        # 隐藏最大化最小化
        self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint | QtCore.Qt.WindowStaysOnTopHint)
        # 添加框组件绑定（任务名，备注） 最大总体布局为纵向布局
        vLayout = QVBoxLayout()
        # 创建填写布局
        # 名称
        vLayout.addWidget(self.unit_name_module())
        # 备注
        vLayout.addWidget(self.unit_code_module())
        # 创建按钮组（确定，取消）
        vLayout.addWidget(self.buttons())
        self.setLayout(vLayout)

    # 添加分组名组件
    def unit_name_module(self):
        widget = QWidget()
        # 任务名
        unit_name_label = QLabel()
        unit_name_label.setText("分组名称")
        # 创建任务名填写框
        unit_name_LineEdit = QLineEdit(self)
        self.unit_name_LineEdit = unit_name_LineEdit
        unit_name_LineEdit.setObjectName("unit_name_LineEdit")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(unit_name_label)
        hLayout1.addWidget(unit_name_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 添加分组编码组件
    def unit_code_module(self):
        widget = QWidget()
        # 任务名
        unit_code_label = QLabel()
        unit_code_label.setText("分组编码")
        # 创建任务名填写框
        unit_code_LineEdit = QLineEdit(self)
        self.unit_code_LineEdit = unit_code_LineEdit
        unit_code_LineEdit.setObjectName("unit_code_LineEdit")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(unit_code_label)
        hLayout1.addWidget(unit_code_LineEdit)

        widget.setLayout(hLayout1)
        return widget

    # 确定取消按钮组
    def buttons(self):
        widget = QWidget()
        # 确定
        confirmBtn = QPushButton('确定',self)
        confirmBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        confirmBtn.clicked.connect(lambda : self.create_unit_confirm())

        # 取消
        cancelBtn = QPushButton('取消')
        cancelBtn.setStyleSheet(''' text-align : center;
                                    background-color : transparent;
                                    height : 25px;
                                    border-style: outset;
                                    padding:0 15px;
                                    font : 13px; ''')

        cancelBtn.clicked.connect(self.close)

        # 确认删除按钮布局
        hLayout = QHBoxLayout()
        hLayout.setObjectName("buttonLayout")
        hLayout.addStretch();
        hLayout.addWidget(confirmBtn)
        hLayout.addWidget(cancelBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 修改分组事件
    def create_unit_confirm(self):

        name = self.findChild(QLineEdit,"unit_name_LineEdit").text()
        code = self.findChild(QLineEdit,"unit_code_LineEdit").text()

        data = {
            "id":self.id,
            "name": name,
            "code": code
        }

        dataMap = requestUtil("/template/unit/update","PUT",data).dataMap

        if dataMap["flag"]:
            # 关闭本窗口
            self.close()
            # 成功提示
            QtWidgets.QMessageBox.about(self ,"修改成功", dataMap["message"])
            # 重新渲染单位及分组下拉列表
            home_bind().ug_comboBox_value_fun()
            # 新增成功刷新数据
            from robotController.binding.template_group_bind import unit_data_bind
            unit_data_bind()
        else:
            QtWidgets.QMessageBox.about(self ,"修改失败", dataMap["message"])